package cn.kwq.project_manger.utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.kwq.project_manger.config.MinioConfig;
import io.minio.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

/**
 * @PackageName: com.hope.minio.utils
 * @ClassName: MinioUtil
 * @Author Hope
 * @Date 2020/7/27 11:43
 * @Description: MinioUtil
 */
@Component
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;
    @Autowired
    S3TypeUtil s3TypeUtil;

    @Autowired
    MinioConfig minioConfig;

    private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;

    /**
     * 检查存储桶是否存在
     *
     * @param bucketName 存储桶名称
     * @return
     */
    @SneakyThrows
    public boolean bucketExists(String bucketName) {
        boolean flag = false;
        flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (flag) {
            return true;
        }
        return false;
    }

    /**
     * 创建存储桶
     *
     * @param bucketName 存储桶名称
     */
    @SneakyThrows
    public boolean makeBucket(String bucketName) {
        boolean flag = bucketExists(bucketName);
        if (!flag) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            return true;
        } else {
            return false;
        }
    }



    /**
     * 通过文件上传到对象
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     *
     * @return
     */
//    @SneakyThrows
//    public boolean putObject(String bucketName, String objectName) {
//        boolean flag = bucketExists(bucketName);
//        if (flag) {
//            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).build());
//            //todo
//
//        }
//        return false;
//    }




    /**
     * 通过InputStream上传图片
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @param stream     要上传的流
     * @param stream     要上传的文件后缀
     * @return
     */
    @SneakyThrows
    public void putImageObject(String bucketName, String objectName, InputStream stream, String suffix) {
        String contentType="application/octet-stream";
        //判断桶存在与否
        boolean flag = bucketExists(bucketName);
        if (flag) {

            if("jpg".equals(suffix) || "JPG".equals(suffix)){
                contentType="image/jpg";
            }
            else if("png".equals(suffix) || "PNG".equals(suffix)){
                contentType="image/png";
            }
            else if("jpeg".equals(suffix) || "JPEG".equals(suffix)){
                contentType="image/jpeg";
            }
            else if("svg".equals(suffix) || "SVG".equals(suffix)){
                contentType="image/svg";
            }
            else if("gif".equals(suffix) || "GIF".equals(suffix)){
                contentType="image/gif";
            }


            minioClient.putObject(PutObjectArgs
                    .builder().stream(stream,stream.available(), -1)
                    .contentType(contentType)
                    .bucket(bucketName)
                    .object(objectName)
                    .build());

    }}




    /**
     *
     * @param bucketName 桶名字
     * @param UUID         uuid
     * @param file        文件
     * @param suffix      后缀
     */
        @SneakyThrows
        public String putOBJ(String bucketName, String UUID, MultipartFile file, String suffix) {
            String contentType = s3TypeUtil.getContentType(suffix);
            //判断桶存在与否
            boolean flag = !bucketExists(bucketName);
            if (flag) {
                throw new RuntimeException("bucketName Error");
            }
            minioClient.putObject(PutObjectArgs
                    .builder()
                    .stream(file.getInputStream(),file.getSize(),-1)
                    .contentType(contentType)
                    .bucket(bucketName)
                    .object(UUID+"."+suffix)
                    .build());
            HashMap<String,String> map =new HashMap<>();
            return minioConfig.getUrl()+"/"+bucketName+"/"+UUID+"."+suffix;
        }

    @SneakyThrows
    public void delImageObject(String url) {
        String[] split = url.split("/");
        String bucketName = split[3];
        String objectName =split[4];

        minioClient.removeObject(RemoveObjectArgs
                .builder()
                .bucket(bucketName)
                .object(objectName)
                .build());

    }
}

